<?php

import("data.db.DbAbstractTable");
import("data.db.DbAbstractForeign");
import("data.db.DbFactory");
import("data.logic.catalogue.Node");
import("data.logic.catalogue.Catalogue");

class DbNodeLink extends DbAbstractTable {
	
	public $className = "data.logic.catalogue.NodeLink";
	const CLASS_NAME = "data.logic.catalogue.NodeLink";
	
	const TB_TABLENAME	= "pd_catalog_link";
	const TB_I			= 'catl';
	
	const TB_TYPE		= 'catl_type';
	
	const TYPE_MAIN		= 'main';
	const TYPE_ALT		= 'alt';
	
	
	public static function _get_TABLENAME() {return self::TB_TABLENAME;}
	public static function _get_ID() {return self::TB_I."_id";}
	public static function _get_SUFFIX() {return self::TB_I;}
	
	function forwardForeignCollection() {
		return array(
			"ChildNode"=>array("data.logic.catalogue.Node","catl_id_catn"),
			"ParentNode"=>array("data.logic.catalogue.Node","catl_id_parent"),
			"Catalogue"=>"data.logic.catalogue.Catalogue"
		);
	}
	
	function backwardForeignCollection() {
		return parent::backwardForeignCollection();
	}
	
	// properties
	public $type;
	
	public function __construct($noinit=false) {
		parent::__construct($noinit);
	}
	
	function setType($type) {$this->type = $type;}
	function getType() {return $this->type;}
	
	function setChildNode(&$node) {
		$this->forwardForeigns["ChildNode"]->setForeign($node);
	}
	function &getChildNode() {
		return $this->forwardForeigns["ChildNode"]->getForeign();
	}
	
	function setParentNode(&$node) {
		$this->forwardForeigns["ParentNode"]->setForeign($node);
	}
	function &getParentNode() {
		return $this->forwardForeigns["ParentNode"]->getForeign();
	}
	
	function setCatalogue(&$cat) {
		$this->forwardForeigns["Catalogue"]->setForeign($cat);
	}
	function &getCatalogue() {
		return $this->forwardForeigns["Catalogue"]->getForeign();
	}
	

	// restore
	function fillEntity($arrResult) {
		parent::fillEntity($arrResult);
		$this->setType($arrResult[self::TB_TYPE]);
	}
	
	//store
	function fillFields() {
		parent::fillFields();
		$this->fildsForStore[self::TB_TYPE] = "'".$this->getType()."'";
	}

	
	function findByName($name) {
		return parent::__selectByCriteria(
			self::TB_NAME."='".$name."'",
			'data.logic.catalogue.Node');
	}
	
	// same for all classes
    public static function findAll() {return parent::findAll(self::CLASS_NAME);}
    //public static function findById($id) {return parent::findById($id, self::CLASS_NAME);}
}
?>